#/usr/bin/tclsh

source [file dirname [info script]]/define.tcl
#exec ./nios_sh
#compile all cpus code
for {set i 0} {$i<$X_NODE_NUM} {incr i} {
	for {set j 0} {$j<$Y_NODE_NUM} {incr j} {
		if { $SDRAM_EN == 1 && $i == $SDRAM_SW_X_ADDR && $j == $SDRAM_SW_Y_ADDR} { 
			puts  "file cpu${i}_${j}.c has been skiped since the router is connected to the sdram"	
		} elseif { $JTAG_INTERFACE_EN == 1 && $i == $JTAG_SW_X_ADDR	 && $j	== $JTAG_SW_Y_ADDR} {
			puts  "file cpu${i}_${j}.c has been skiped since the router is connected to the jtag"
		} else {
			#exec cp code/cpu.c	code/cpu${i}_${j}.c
			exec cp code/cpu${i}_${j}.c	compile/code.c
			exec make -C compile
			puts  "file cpu${i}_${j}.c has been compiled"	
			exec cp compile/compile.elf	code/elf/cpu${i}_${j}.elf
		}

}}

# programe all nios
for {set i 0} {$i<$X_NODE_NUM} {incr i} {
	for {set j 0} {$j<$Y_NODE_NUM} {incr j} {	
		if { $SDRAM_EN == 1 && $i == $SDRAM_SW_X_ADDR && $j == $SDRAM_SW_Y_ADDR} { 
			
		} elseif { $JTAG_INTERFACE_EN	== 1	&& $i == $JTAG_SW_X_ADDR	 && $j	== $JTAG_SW_Y_ADDR} {	

		} else {	
			set result [catch {exec quartus_stp -t prog.tcl -x_axe $i -y_axe $j -filename  code/elf/cpu${i}_${j}.elf } ]
			if { $result == 1 } {				
				puts "------------------------------Failed to programe cpu${i}_${j} !----------------------------------" 
			} else {
				puts  "cpu${i}_${j} has been programed successfully!"				
			}	
			#puts $result			
		}
		

}}











